home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / snip9_91.arc / FLNORM.C < prev    next >
C/C++ Source or Header  |  1991-09-17  |  4KB  |  146 lines

  1. /*
  2. **  FLNORM.C - Normalize DOS file names
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #ifdef __TURBOC__
  8.  #include <dir.h>
  9. #else
  10.  #include <direct.h>
  11. #endif
  12. #include <dos.h>
  13. #include <io.h>
  14.  
  15. #define MAX_FLEN 67
  16. #define LAST_CHAR(string) (((char *)string)[strlen(string)-1])
  17.  
  18. typedef enum {ERROR = -1, FALSE, TRUE} LOGICAL;
  19.  
  20. char *unix2dos(char *path);
  21.  
  22. int flnorm(char *in_name, char *out_name)
  23. {
  24.       LOGICAL dir_flag = FALSE, new_drv = FALSE, root_flag;
  25.       int status = 0, level = 0;
  26.       char *p, *out;
  27.       static char drive[2][3];
  28.       static char file[14];
  29.       static char I_am_here[MAX_FLEN];
  30.       static char I_am_there[MAX_FLEN];
  31.       static char remember[MAX_FLEN];
  32.  
  33.       getcwd(I_am_here, MAX_FLEN);
  34.       if (!in_name || !in_name[0])
  35.       {
  36.             strcpy(out_name, I_am_here);
  37.             goto ERRexit;
  38.       }
  39.       strncpy(drive[0], I_am_here, 2);
  40.       drive[0][2] = '\0';
  41.       if (':' == in_name[1])
  42.       {     /* If a drive is specified                            */
  43.             if (chdrv(in_name[0]))
  44.             {     /* If the drive is invalid                      */
  45.                   status = ERROR;
  46.                   goto ERRexit;
  47.             }
  48.             new_drv = TRUE;
  49.             getcwd(remember, MAX_FLEN);
  50.             strncpy(drive[1], remember, 2);
  51.             drive[1][2] = '\0';
  52.       }
  53.       else
  54.       {     /* If a drive isn't specified                         */
  55.             if (NULL != (p = strchr(in_name, ':')))
  56.             {     /* If filename is illegal                       */
  57.                   status = ERROR;
  58.                   goto ERRexit;
  59.             }
  60.       }
  61.       unix2dos(in_name);
  62.       if (new_drv)
  63.       {
  64.             if ('\\' == in_name[2])
  65.                   strcpy(out_name, drive[1]);
  66.             else
  67.             {
  68.                   strcpy(out_name, remember);
  69.                   if ('\\' != LAST_CHAR(remember))
  70.                         strcat(out_name, "\\");
  71.             }
  72.       }
  73.       else
  74.       {
  75.             strcpy(out_name, drive[0]);
  76.             if ('\\' != *in_name)
  77.             {
  78.                   strcat(out_name, I_am_here);
  79.                   if ('\\' != LAST_CHAR(I_am_here))
  80.                         strcat(out_name, "\\");
  81.             }
  82.       }
  83.       strcat(out_name, &in_name[(new_drv) ? 2 : 0]);
  84.       fln_fix(out_name);
  85.       out = &out_name[2];
  86.       if (!(*out))
  87.             goto ERRexit;
  88.       while ('\\' == LAST_CHAR(out))
  89.       {     /* Strip trailing `\'s                                */
  90.             LAST_CHAR(out_name) = '\0';
  91.             dir_flag = TRUE;
  92.       }
  93.       if (!(*out))
  94.       {
  95.             if (dir_flag)
  96.             {
  97.                   strcat(out, "\\");
  98.                   goto ERRexit;
  99.             }
  100.             else  goto BADPATH;
  101.       }
  102.       if (NULL != (p = strrchr(out_name, '\\')))
  103.             strcpy(file, p);        /* Save filename              */
  104.       if (chdir(out))
  105.       {     /* If can't move to path                              */
  106.             if ((!dir_flag) && p)
  107.             {     /* If there was a separate path                 */
  108.                   *p = '\0';
  109.                   if (!(*out))
  110.                   {     /* Back at the root, handle it            */
  111.                         strcpy(p, "\\");
  112.                         strcpy(file, &file[1]);
  113.                   }
  114.                   if (chdir(out))
  115.                   {     /* If we can't move to path               */
  116.                         *p = '\\';
  117.                         goto BADPATH;
  118.                   }
  119.                   ++level;          /* Flag we stripped name      */
  120.             }
  121.             else
  122.             {     /* No path as specified                         */
  123.                   if (p)
  124.                   {
  125. BADPATH:                status = ERROR;
  126.                         goto ERRexit;
  127.                   }
  128.             }
  129.       }
  130.       getcwd(I_am_there, MAX_FLEN); /* Get normalized path        */
  131.       strupr(I_am_there);
  132.       strcpy(out_name, I_am_there);
  133.       if (level)
  134.             strcat(out_name, file);
  135. ERRexit:
  136.       if (new_drv)
  137.       {
  138.             chdir(remember);
  139.             chdrv(I_am_here[0]);
  140.       }
  141.       chdir(I_am_here);
  142.       if (status)
  143.             out_name[0] = '\0';
  144.       return status;
  145. }
  146.